<?php

/**
 * 商品数据汇总统计
 * @author jincai
 */
namespace app\statis\model;

use app\common\model\StatProduct;
use app\statis\lib\TimeStatistic;
use think\Exception;

class StatProductModel extends StatProduct
{
    /**
     * 商品数据汇总统计
     *
     * @param $start_time
     * @param $end_time
     * @throws Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function summary($start_time, $end_time)
    {
        $coreM      = new CoreOrderModel();
        $productM   = new ProductModel();

        $stat_data  = [
            'stat_start_time'   => $start_time,
            'stat_end_time'     => $end_time,
            'stat_time'         => TimeStatistic::getStatTime($end_time),
            'upper_shelf_num'   => 0,
            'lower_shelf_num'   => 0,
            'order_product_num' => 0,
            'total_amount'      => 0,
            'buyer_num'         => 0,
            'sales'             => 0,
            'create_time'       => time(),
        ];

        /*订单商品数据*/
        $order_product_list = $coreM->getProductByTime($start_time, $end_time);
        if (empty($order_product_list)) {
            if (!$this->insertGetId($stat_data)) {
                throw new Exception('插入商品汇总记录失败');
            }
            return;
        }
        /*获取订单商品数，和订单总额，下单人数，销量*/
        $stat_order_data    = $this->statOrder($order_product_list);
        /*获取上架商品数，下架商品数*/
        $stat_product_data  = $productM->statProduct();

        /*组织入库数据*/
        $stat_data['upper_shelf_num']   = $stat_product_data['upper_shelf_num'];
        $stat_data['lower_shelf_num']   = $stat_product_data['lower_shelf_num'];
        $stat_data['order_product_num'] = $stat_order_data['order_product_num'];
        $stat_data['total_amount']      = $stat_order_data['total_amount'];
        $stat_data['buyer_num']         = $stat_order_data['buyer_num'];
        $stat_data['sales']             = $stat_order_data['sales'];

        if (!$this->insertGetId($stat_data)) {
            throw new Exception('插入商品汇总记录失败');
        }
    }

    /**
     * 获取统计订单商品数，订单总额，
     * 下单人数，销量的数据
     *
     * @param $order_product
     * @return array
     */
    public function statOrder($order_product)
    {
        $total_amount       = 0;
        $user               = [];
        $sales              = 0;
        $buyer_num          = 0;
        $order_product_num  = 0;
        $order_product_list = [];

        foreach ($order_product as $k => $order) {
            if (!isset($user[$order['user_id']])) {
                $user[$order['user_id']] = 0;
            }
            $user[$order['user_id']] += 1;

            foreach ($order['product'] as $i => $p) {
                $total_amount   += ($p['price'] * $p['number']);
                $sales          += $p['number'];
                /*订单商品数*/
                if (!isset($order_product_list[$p['product_id']])) {
                    $order_product_list[$p['product_id']] = 0;
                }
                $order_product_list[$p['product_id']] += 1;
            }
        }

        $buyer_num          = count($user);
        $order_product_num  = count($order_product_list);

        $data = [
            'total_amount'      => bcmul($total_amount, 100, 2), /*商品数*价格*/
            'sales'             => $sales, /*销售量*/
            'buyer_num'         => $buyer_num, /*购买人数*/
            'order_product_num' => $order_product_num, /*下单商品数*/
        ];

        return $data;
    }

}